package bsc.war.form;



import bsc.ejb.entity.ScoreCard;
import bsc.ejb.entity.ScoreCardRelation;
import bsc.war.delegate.DelegateBSC;
import bsc.war.factory.Factory;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 *
 * @author memozac
 */
public class NewScoreCardRelation extends AbstractController  {

     @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
         HttpServletResponse response) throws Exception {
        
        try {
            DelegateBSC delegate = (DelegateBSC) Factory.getBean("DelegateBSC");
            Long idScoreCard = request.getParameter("scoreCard") != null && NumberUtils.isNumber(request.getParameter("scoreCard")) ? Long.parseLong(request.getParameter("scoreCard")) : null;
            ScoreCard parentScoreCard = delegate.findScoreCard(idScoreCard);
            String[] childrenScoreCardsId = request.getParameterValues("childrenScoreCard");
            // System.out.println("Parent: "+idScoreCard+" ChildrenScoreCards: "+childrenScoreCardsId.length);
            if(childrenScoreCardsId != null && childrenScoreCardsId.length > 0)
                for (String idChildScoreCard : childrenScoreCardsId) {
                    if (!NumberUtils.isNumber(idChildScoreCard)) continue;
                    
                    ScoreCardRelation scoreCardRelation = delegate.findScoreCardRelation(idScoreCard, Long.parseLong(idChildScoreCard));
                    if (scoreCardRelation == null) {
                        scoreCardRelation = (ScoreCardRelation) Factory.getBean("ScoreCardRelation");
                        ScoreCard childScoreCard = delegate.findScoreCard(Long.parseLong(idChildScoreCard));
                        scoreCardRelation.setScoreCard(parentScoreCard);
                        scoreCardRelation.setOtherScoreCard(childScoreCard);
                        scoreCardRelation.setPercentFactor(Double.parseDouble(request.getParameter("percentFactor_" + idChildScoreCard)));

                        List<ScoreCard> scoreCardsChild= delegate.findScoreCard(childScoreCard);
                        if(!scoreCardsChild.contains(parentScoreCard)){
                            delegate.createScoreCardRelation(scoreCardRelation);
                        }else{
                            request.setAttribute("result", "Observaciones: No se pueden agregar elementos que ya contienen al padre.");
                        }

                        
                    } else {
                        scoreCardRelation.setPercentFactor(Double.parseDouble(request.getParameter("percentFactor_" + idChildScoreCard)));
                        delegate.updateScoreCardRelation(scoreCardRelation);
                    }
                }

            String[] toRemove = request.getParameterValues("removeRelation");
            if(toRemove != null && toRemove.length > 0)
            for(String rr : toRemove){
                if(!NumberUtils.isNumber(rr)) continue;
                ScoreCardRelation relation = delegate.findScoreCardRelation(idScoreCard, Long.parseLong(rr));
                if(relation != null) delegate.deleteScoreCardRelation(relation);
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return new ModelAndView("newScoreCardRelation");
    }

}
